home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / file-ops.cc < prev    next >
C/C++ Source or Header  |  1997-08-20  |  7KB  |  384 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. /* Modified by Klaus Gebhardt, 1996 */
  24.  
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28.  
  29. #include <cerrno>
  30. #include <cstdio>
  31. #include <cstdlib>
  32. #include <cstring>
  33.  
  34. #ifdef HAVE_UNISTD_H
  35. #ifdef HAVE_SYS_TYPES_H
  36. #include <sys/types.h>
  37. #endif
  38. #include <unistd.h>
  39. #endif
  40.  
  41. #ifdef __EMX__
  42. #include <mkfifo.h>
  43. #endif
  44.  
  45. #include "file-ops.h"
  46. #include "lo-error.h"
  47. #include "statdefs.h"
  48.  
  49. // These must come after <sys/types.h> and <sys/stat.h>.
  50.  
  51. #include <safe-lstat.h>
  52. #include <safe-stat.h>
  53.  
  54. // XXX FIXME XXX -- the is_* and mode_as_string functions are only valid
  55. // for initialized objects.  If called for an object that is not
  56. // initialized, they should throw an exception.
  57.  
  58. bool
  59. file_stat::is_blk (void) const
  60. {
  61. #ifdef S_ISBLK
  62.   return S_ISBLK (fs_mode);
  63. #else
  64.   return false;
  65. #endif
  66. }
  67.  
  68. bool
  69. file_stat::is_chr (void) const
  70. {
  71. #ifdef S_ISCHR
  72.   return S_ISCHR (fs_mode);
  73. #else
  74.   return false;
  75. #endif
  76. }
  77.  
  78. bool
  79. file_stat::is_dir (void) const
  80. #ifdef S_ISDIR
  81.   return S_ISDIR (fs_mode);
  82. #else
  83.   return false;
  84. #endif
  85. }
  86.  
  87. bool
  88. file_stat::is_fifo (void) const
  89. #ifdef S_ISFIFO
  90.   return S_ISFIFO (fs_mode);
  91. #else
  92.   return false;
  93. #endif
  94. }
  95.  
  96. bool
  97. file_stat::is_lnk (void) const
  98. #ifdef S_ISLNK
  99.   return S_ISLNK (fs_mode);
  100. #else
  101.   return false;
  102. #endif
  103. }
  104.  
  105. bool
  106. file_stat::is_reg (void) const
  107. #ifdef S_ISREG
  108.   return S_ISREG (fs_mode);
  109. #else
  110.   return false;
  111. #endif
  112. }
  113.  
  114. bool
  115. file_stat::is_sock (void) const
  116. #ifdef S_ISSOCK
  117.   return S_ISSOCK (fs_mode);
  118. #else
  119.   return false;
  120. #endif
  121. }
  122.  
  123. extern "C" void mode_string ();
  124.  
  125. string
  126. file_stat::mode_as_string (void) const
  127. {
  128.   char buf[11];
  129.  
  130.   mode_string (fs_mode, buf);
  131.  
  132.   buf[10] = '\0';
  133.  
  134.   return string (buf);
  135. }
  136.  
  137. // Private stuff:
  138.  
  139. void
  140. file_stat::update_internal (bool force)
  141. {
  142.   if (! initialized || force)
  143.     {
  144.       initialized = false;
  145.       fail = false;
  146.  
  147.       const char *cname = file_name.c_str ();
  148.  
  149.       struct stat buf;
  150.  
  151.       int status = follow_links
  152.     ? SAFE_STAT (cname, &buf) : SAFE_LSTAT (cname, &buf);
  153.  
  154.       if (status < 0)
  155.     {
  156.       fail = true;
  157.       errmsg = strerror (errno);
  158.     }
  159.       else
  160.     {
  161.       fs_mode = buf.st_mode;
  162.       fs_ino = buf.st_ino;
  163.       fs_dev = buf.st_dev;
  164.       fs_nlink = buf.st_nlink;
  165.       fs_uid = buf.st_uid;
  166.       fs_gid = buf.st_gid;
  167.       fs_size = buf.st_size;
  168.       fs_atime = buf.st_atime;
  169.       fs_mtime = buf.st_mtime;
  170.       fs_ctime = buf.st_ctime;
  171.  
  172. #if defined (HAVE_ST_RDEV)
  173.       fs_rdev = buf.st_rdev;
  174. #endif
  175.  
  176. #if defined (HAVE_ST_BLKSIZE)
  177.       fs_blksize = buf.st_blksize;
  178. #endif
  179.  
  180. #if defined (HAVE_ST_BLOCKS)
  181.       fs_blocks = buf.st_blocks;
  182. #endif
  183.     }
  184.  
  185.       initialized = true;
  186.     }
  187. }
  188.  
  189. void
  190. file_stat::copy (const file_stat& fs)
  191. {
  192.   file_name = fs.file_name;
  193.   follow_links = fs.follow_links;
  194.   initialized = fs.initialized;
  195.   fail = fs.fail;
  196.   errmsg = fs.errmsg;
  197.   fs_mode = fs.fs_mode;
  198.   fs_ino = fs.fs_ino;
  199.   fs_dev = fs.fs_dev;
  200.   fs_nlink = fs.fs_nlink;
  201.   fs_uid = fs.fs_uid;
  202.   fs_gid = fs.fs_gid;
  203.   fs_size = fs.fs_size;
  204.   fs_atime = fs.fs_atime;
  205.   fs_mtime = fs.fs_mtime;
  206.   fs_ctime = fs.fs_ctime;
  207.  
  208. #if defined (HAVE_ST_RDEV)
  209.   fs_rdev = fs.fs_rdev;
  210. #endif
  211.  
  212. #if defined (HAVE_ST_BLKSIZE)
  213.   fs_blksize = fs.fs_blksize;
  214. #endif
  215.  
  216. #if defined (HAVE_ST_BLOCKS)
  217.   fs_blocks = fs.fs_blocks;
  218. #endif
  219. }
  220.  
  221. // Functions for octave.
  222.  
  223. // Has FILE been modified since TIME?  Returns 1 for yes, 0 for no,
  224. // and -1 for any error.
  225. int
  226. is_newer (const string& file, time_t time)
  227. {
  228.   file_stat fs (file);
  229.  
  230.   return fs ? fs.is_newer (time) : -1;
  231. }
  232.  
  233. // We provide a replacement for mkdir().
  234.  
  235. int
  236. oct_mkdir (const string& name, mode_t mode)
  237. {
  238.   return mkdir (name.c_str (), mode);
  239. }
  240.  
  241. int
  242. oct_mkdir (const string& name, mode_t mode, string& msg)
  243. {
  244.   msg = string ();
  245.  
  246.   int status = mkdir (name.c_str (), mode);
  247.  
  248.   if (status < 0)
  249.     msg = strerror (errno);
  250.  
  251.   return status;
  252. }
  253.  
  254. // I don't know how to emulate this on systems that don't provide it.
  255.  
  256. int
  257. oct_mkfifo (const string& name, mode_t mode)
  258. {
  259.   string msg;
  260.   return oct_mkfifo (name, mode, msg);
  261. }
  262.  
  263. int
  264. oct_mkfifo (const string& name, mode_t mode, string& msg)
  265. {
  266.   msg = string ();
  267.  
  268. #if defined (HAVE_MKFIFO)
  269.   int status = mkfifo (name.c_str (), mode);
  270.  
  271.   if (status < 0)
  272.     msg = strerror (errno);
  273.  
  274.   return status;
  275. #else
  276.   msg = "mkfifo: not implemented on this system";
  277.   return -1;
  278. #endif
  279. }
  280.  
  281. // We provide a replacement for rename().
  282.  
  283. int
  284. oct_rename (const string& from, const string& to)
  285. {
  286.   return rename (from.c_str (), to.c_str ());
  287. }
  288.  
  289. int
  290. oct_rename (const string& from, const string& to, string& msg)
  291. {
  292.   msg = string ();
  293.  
  294.   int status = rename (from.c_str (), to.c_str ());
  295.  
  296.   if (status < 0)
  297.     msg = strerror (errno);
  298.  
  299.   return status;
  300. }
  301.  
  302. // We provide a replacement for rmdir().
  303.  
  304. int
  305. oct_rmdir (const string& name)
  306. {
  307.   return rmdir (name.c_str ());
  308. }
  309.  
  310. int
  311. oct_rmdir (const string& name, string& msg)
  312. {
  313.   msg = string ();
  314.  
  315.   int status = rmdir (name.c_str ());
  316.  
  317.   if (status < 0)
  318.     msg = strerror (errno);
  319.  
  320.   return status;
  321. }
  322.  
  323. // We provide a replacement for tempnam().
  324.  
  325. string
  326. oct_tempnam (void)
  327. {
  328.   string retval;
  329.  
  330.   char *tmp = tempnam (0, "oct-");
  331.  
  332.   if (tmp)
  333.     {
  334.       retval = tmp;
  335.  
  336.       free (tmp);
  337.     }
  338.   else
  339.     (*current_liboctave_error_handler) ("can't open temporary file!");
  340.  
  341.   return retval;
  342. }
  343.  
  344.  
  345. int
  346. oct_umask (mode_t mode)
  347. {
  348. #if defined (HAVE_UMASK)
  349.   return umask (mode);
  350. #else
  351.   return 0;
  352. #endif
  353. }
  354.  
  355. int
  356. oct_unlink (const string& name)
  357. {
  358.   return unlink (name.c_str ());
  359. }
  360.  
  361. int
  362. oct_unlink (const string& name, string& errmsg)
  363. {
  364.   errmsg = string ();
  365.  
  366.   int status = unlink (name.c_str ());
  367.  
  368.   if (status < 0)
  369.     errmsg = strerror (errno);
  370.  
  371.   return status;
  372. }
  373.  
  374. /*
  375. ;;; Local Variables: ***
  376. ;;; mode: C++ ***
  377. ;;; End: ***
  378. */
  379.